﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Web.Security;
using BussinessSMS;
using DataAccessSMS;


public partial class Account_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        

    }
    public static ProfileBase GetProfileBaseObj()
    {
        ProfileBase proBase = (ProfileBase)HttpContext.Current.Profile;
        return proBase;
    }
    public static CommonCls GetGeneralObj()
    {
        CommonCls GeneralObj = new CommonCls(GetProfileBaseObj().GetPropertyValue("UserId").ToString());
        return GeneralObj;
    }
    protected void Loginbtn_Click(object sender, EventArgs e)
    {        
        string UserId = txtname.Value.Trim();
        string Password = txtpw.Value.Trim();
        List<User> userInfoList = GetGeneralObj().GetUserInfo(UserId, Password);
        if (userInfoList.Count() > 0)
        {
            Dictionary<string, string> userInfo = new Dictionary<string, string>();
            userInfo.Add("UserId", userInfoList[0].User_Id);
            userInfo.Add("FirstName", userInfoList[0].First_Name);
            userInfo.Add("LastName", userInfoList[0].Last_Name);
            userInfo.Add("UserType", userInfoList[0].User_Type);
            userInfo.Add("SchoolId", userInfoList[0].School_Id.ToString());

            if (!Membership.ValidateUser(UserId.ToLower(), Password.ToLower()))
            {
                try
                {
                    Membership.CreateUser(UserId.ToLower(), Password.ToLower());
                }
                catch
                {
                    ;
                }
            }

            UpdateProfile(userInfo);
           FormsAuthentication.SetAuthCookie(UserId, false);
            Response.Redirect("~/admin/DashBoard.aspx");
        }
        else
        {
            divMsg.Style.Add(HtmlTextWriterStyle.Display, "block");
        }
        
    }

    private void UpdateProfile(Dictionary<string, string> userInfo)
    {
        MembershipUser mu = Membership.GetUser(userInfo["UserId"].ToLower());
        ProfileCommon Pc = Profile.GetProfile(mu.UserName.ToLower());
        Pc.UserId = userInfo["UserId"];
        Pc.FirstName = userInfo["FirstName"];
        Pc.LastName = userInfo["LastName"];
        Pc.UserType = userInfo["UserType"];
        Pc.SchoolID = userInfo["SchoolId"];
        Pc.Save();
        
    }
}